<template>
  <div>
    <!-- 搜索页-点击按钮 -->
    <form action="/">
      <van-search
        v-model="value"
        shape="round"
        placeholder="请输入搜索关键词"
        @search="onSearch()"
      />
    </form>
    <!--<p>{{$route.params.userId}}</p>-->
    <div>
      <div class="line">搜索结果</div>
      <p v-if="isNull" class="dialog">暂无搜索结果</p>
      <div v-for="(item, index) in searchList" :key="index" class="song-div">
        <p class="song-name">{{ item.name }}</p>
        <p class="artist-name">
          <i class="sq"></i>
          <span v-for="(artist, idx) in item.artists" :key="'artist' + idx">
            {{ artist.name }}
            <!-- 歌手是多人时，添加分割线 -->
            <span
              v-if="item.artists.length > 1 && idx != item.artists.length - 1"
              >/</span
            >
          </span>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      //搜索结果
      searchList: [],
      isNull: true,
    };
  },
  created() {
    //向父组件（index.vue）传值
    this.$emit('transferUser', true)
    this.value = this.$route.params.userId;
    this.getSearch();
  },
  methods: {
    //点击搜索
    onSearch() {
      this.getSearch();
    },
    //搜索结果
    getSearch() {
      if (this.value != null) {
        this.isNull = false;
        let keywords = "/search/suggest?keywords=" + this.value;
        console.log(keywords);
        this.$http(keywords)
          .then((res) => {
            console.log(res);
            this.searchList = res.data.result.songs;
            //console.log(this.searchList)
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
      }
    },
  },
};
</script>

<style>
.line {
  height: 20px;
  line-height: 20px;
  border-left: 2px solid #f00;
  margin-top: 10px;
  padding-left: 10px;
  font-size: 18px;
}
.song-div {
  border-bottom: 1px solid #f4f4f4;
  padding-left: 10px;
}
.song-name {
  font-size: 16px;
}
.artist-name {
  font-size: 12px;
  color: #666;
}
.sq {
  display: inline-block;
  width: 14px;
  height: 10px;
  background-image: url("../../static/img/index_icon_2x.png");
  background-size: 186.75px 109.125px;
  background-position: 0 0;
}
.dialog {
  width: 100%;
  height: 10vh;
  top: 220px;
  position: fixed;
  font-size: 20px;
  text-align: center;
  z-index: 6;
}
</style>